﻿/**  版本信息模板在安装目录下，可自行修改。
* BlogInfo.cs
*
* 功 能： N/A
* 类 名： BlogInfo
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2014/9/29 17:13:43   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using DBUtility;
namespace BlogDH.DAL
{
    /// <summary>
    /// 数据访问类:BlogInfo
    /// </summary>
    public partial class BlogInfo
    {
        public BlogInfo()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string BlogUrl)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BlogInfo");
            strSql.Append(" where BlogUrl=@BlogUrl ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@BlogUrl", OleDbType.VarChar,255)			};
            parameters[0].Value = BlogUrl;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BlogDH.Model.BlogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BlogInfo(");
            strSql.Append("BlogUrl,BlogName,BlogTags,BlogDesc,CreateYear,CreateMouth,IsShow,IsWWW,GoCount,ComeCount,UpdateTime,CreateTime,UserID,State,sheng,shi,xian)");
            strSql.Append(" values (");
            strSql.Append("@BlogUrl,@BlogName,@BlogTags,@BlogDesc,@CreateYear,@CreateMouth,@IsShow,@IsWWW,@GoCount,@ComeCount,@UpdateTime,@CreateTime,@UserID,@State,@Sheng,@Shi,@Xian)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@BlogUrl", OleDbType.VarChar,255),
					new OleDbParameter("@BlogName", OleDbType.VarChar,255),
					new OleDbParameter("@BlogTags", OleDbType.VarChar,255),
					new OleDbParameter("@BlogDesc", OleDbType.VarChar,255),
					new OleDbParameter("@CreateYear", OleDbType.Integer,4),
					new OleDbParameter("@CreateMouth", OleDbType.Integer,4),
					new OleDbParameter("@IsShow", OleDbType.Integer,4),
					new OleDbParameter("@IsWWW", OleDbType.Integer,4),
					new OleDbParameter("@GoCount", OleDbType.Integer,4),
					new OleDbParameter("@ComeCount", OleDbType.Integer,4),
					new OleDbParameter("@UpdateTime", OleDbType.Date),
					new OleDbParameter("@CreateTime", OleDbType.Date),
					new OleDbParameter("@UserID", OleDbType.Integer,4),
					new OleDbParameter("@State" , OleDbType.Integer,4),
					new OleDbParameter("@Sheng", OleDbType.Integer,4),
					new OleDbParameter("@Shi", OleDbType.Integer,4),
					new OleDbParameter("@Xian", OleDbType.Integer,4)};
            parameters[0].Value = model.BlogUrl;
            parameters[1].Value = CommMethod.HtmlData(model.BlogName);
            parameters[2].Value = CommMethod.HtmlData(model.BlogTags);
            parameters[3].Value = CommMethod.HtmlData(model.BlogDesc);
            parameters[4].Value = model.CreateYear;
            parameters[5].Value = model.CreateMouth;
            parameters[6].Value = model.IsShow;
            parameters[7].Value = model.IsWWW;
            parameters[8].Value = model.GoCount;
            parameters[9].Value = model.ComeCount;
            parameters[10].Value = model.UpdateTime;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.Userid;
            parameters[13].Value = model.State;
            parameters[14].Value = model.Sheng;
            parameters[15].Value = model.Shi;
            parameters[16].Value = model.Xian;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BlogDH.Model.BlogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BlogInfo set ");
            System.Collections.Generic.List<OleDbParameter> list = new System.Collections.Generic.List<OleDbParameter>();

            if (!string.IsNullOrEmpty(model.BlogName))
            {
                strSql.Append("BlogName=@BlogName,");
                list.Add(new OleDbParameter("@BlogName", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = CommMethod.HtmlData(model.BlogName);
            }
            if (!string.IsNullOrEmpty(model.BlogTags))
            {
                strSql.Append("BlogTags=@BlogTags,");
                list.Add(new OleDbParameter("@BlogTags", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = CommMethod.HtmlData(model.BlogTags);
            }
            if (!string.IsNullOrEmpty(model.BlogDesc))
            {
                strSql.Append("BlogDesc=@BlogDesc,");
                list.Add(new OleDbParameter("@BlogDesc", OleDbType.VarChar, 255));
                list[list.Count - 1].Value = CommMethod.HtmlData(model.BlogDesc);
            }
            if (model.CreateYear != null && model.CreateYear != 0)
            {
                strSql.Append("CreateYear=@CreateYear,");
                list.Add(new OleDbParameter("@CreateYear", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.CreateYear;
            }
            if (model.CreateMouth != null && model.CreateMouth != 0)
            {
                strSql.Append("CreateMouth=@CreateMouth,");
                list.Add(new OleDbParameter("@CreateMouth", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.CreateMouth;
            }
            if (model.Sheng != null && model.Sheng != 0)
            {
                strSql.Append("sheng=@sheng,");
                list.Add(new OleDbParameter("@sheng", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Sheng;
            }
            if (model.Shi != null && model.Shi != 0)
            {
                strSql.Append("Shi=@Shi,");
                list.Add(new OleDbParameter("@Shi", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Shi;
            }
            if (model.Xian != null && model.Xian != 0)
            {
                strSql.Append("Xian=@Xian,");
                list.Add(new OleDbParameter("@Xian", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.Xian;
            }
            if (model.IsShow != null)
            {
                strSql.Append("IsShow=@IsShow,");
                list.Add(new OleDbParameter("@IsShow", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.IsShow;
            }
            if (model.IsWWW != null)
            {
                strSql.Append("IsWWW=@IsWWW,");
                list.Add(new OleDbParameter("@IsWWW", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.IsWWW;
            }
            if (model.GoCount != null && model.GoCount != 0)
            {
                strSql.Append("GoCount=@GoCount,");
                list.Add(new OleDbParameter("@GoCount", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.GoCount;
            }
            if (model.ComeCount != null && model.ComeCount != 0)
            {
                strSql.Append("ComeCount=@ComeCount,");
                list.Add(new OleDbParameter("@ComeCount", OleDbType.Integer, 4));
                list[list.Count - 1].Value = model.ComeCount;
            }

            strSql.Append("UpdateTime=@UpdateTime");
            list.Add(new OleDbParameter("@UpdateTime", OleDbType.Date));
            list[list.Count - 1].Value = System.DateTime.Now;

            strSql.Append(" where UserID=@UserID and BlogUrl=@BlogUrl");
            list.Add(new OleDbParameter("@UserID", OleDbType.Integer, 4));
            list[list.Count - 1].Value = model.Userid;
            list.Add(new OleDbParameter("@BlogUrl", OleDbType.VarChar, 255));
            list[list.Count - 1].Value = model.BlogUrl;

            OleDbParameter[] parameters = new OleDbParameter[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                parameters[i] = list[i];
            }

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update2(BlogDH.Model.BlogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BlogInfo set ");
            System.Collections.Generic.List<OleDbParameter> list = new System.Collections.Generic.List<OleDbParameter>();

            strSql.Append("State=@State,");
            list.Add(new OleDbParameter("@State", OleDbType.Integer, 4));
            list[list.Count - 1].Value = model.State;

            strSql.Append("goCount=@goCount");
            list.Add(new OleDbParameter("@goCount", OleDbType.Integer, 4));
            list[list.Count - 1].Value = model.GoCount;

            strSql.Append(" where BlogUrl=@BlogUrl");
            list.Add(new OleDbParameter("@BlogUrl", OleDbType.VarChar, 255));
            list[list.Count - 1].Value = model.BlogUrl;

            OleDbParameter[] parameters = new OleDbParameter[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                parameters[i] = list[i];
            }

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int Update(string sql)
        {
            int rows = DbHelperOleDb.ExecuteSql(sql);
            return rows;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string BlogUrl)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BlogInfo ");
            strSql.Append(" where BlogUrl=@BlogUrl ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@BlogUrl", OleDbType.VarChar,255)			};
            parameters[0].Value = BlogUrl;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string BlogUrllist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BlogInfo ");
            strSql.Append(" where BlogUrl in (" + BlogUrllist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.BlogInfo GetModel(string BlogUrl)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BlogID,BlogUrl,BlogName,sheng,shi,xian,BlogTags,BlogDesc,CreateYear,CreateMouth,IsShow,IsWWW,GoCount,ComeCount,UpdateTime,state,CreateTime from BlogInfo ");
            strSql.Append(" where BlogUrl=@BlogUrl ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@BlogUrl", OleDbType.VarChar,255)			};
            parameters[0].Value = BlogUrl;

            BlogDH.Model.BlogInfo model = new BlogDH.Model.BlogInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.BlogInfo GetModel(int userID, string BlogUrl)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BlogID,BlogUrl,BlogName,BlogTags,sheng,shi,xian,userid,BlogDesc,CreateYear,CreateMouth,IsShow,IsWWW,GoCount,ComeCount,state,UpdateTime,CreateTime from BlogInfo ");
            strSql.Append(" where BlogUrl=@BlogUrl and UserID=@UserID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@BlogUrl", OleDbType.VarChar,255),
					new OleDbParameter("@UserID", OleDbType.VarChar,255)};
            parameters[0].Value = BlogUrl;
            parameters[1].Value = userID;

            BlogDH.Model.BlogInfo model = new BlogDH.Model.BlogInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BlogDH.Model.BlogInfo DataRowToModel(DataRow row)
        {
            BlogDH.Model.BlogInfo model = new BlogDH.Model.BlogInfo();
            if (row != null)
            {
                if (row["BlogID"] != null && row["BlogID"].ToString() != "")
                {
                    model.BlogID = int.Parse(row["BlogID"].ToString());
                }
                if (row["sheng"] != null && row["sheng"].ToString() != "")
                {
                    model.Sheng = int.Parse(row["sheng"].ToString());
                }
                if (row["shi"] != null && row["shi"].ToString() != "")
                {
                    model.Shi = int.Parse(row["shi"].ToString());
                }
                if (row["xian"] != null && row["xian"].ToString() != "")
                {
                    model.Xian = int.Parse(row["xian"].ToString());
                }
                if (row["BlogUrl"] != null)
                {
                    model.BlogUrl = row["BlogUrl"].ToString();
                }
                if (row["BlogName"] != null)
                {
                    model.BlogName = row["BlogName"].ToString();
                }
                if (row["BlogTags"] != null)
                {
                    model.BlogTags = row["BlogTags"].ToString();
                }
                if (row["BlogDesc"] != null)
                {
                    model.BlogDesc = row["BlogDesc"].ToString();
                }
                if (row["CreateYear"] != null && row["CreateYear"].ToString() != "")
                {
                    model.CreateYear = int.Parse(row["CreateYear"].ToString());
                }
                if (row["CreateMouth"] != null && row["CreateMouth"].ToString() != "")
                {
                    model.CreateMouth = int.Parse(row["CreateMouth"].ToString());
                }
                if (row["IsShow"] != null && row["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(row["IsShow"].ToString());
                }
                if (row["IsWWW"] != null && row["IsWWW"].ToString() != "")
                {
                    model.IsWWW = int.Parse(row["IsWWW"].ToString());
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["GoCount"] != null && row["GoCount"].ToString() != "")
                {
                    model.GoCount = int.Parse(row["GoCount"].ToString());
                }
                if (row["ComeCount"] != null && row["ComeCount"].ToString() != "")
                {
                    model.ComeCount = int.Parse(row["ComeCount"].ToString());
                }
                if (row["UpdateTime"] != null && row["UpdateTime"].ToString() != "")
                {
                    model.UpdateTime = DateTime.Parse(row["UpdateTime"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BlogID,BlogUrl,BlogName,State,sheng,shi,xian,BlogTags,BlogDesc,CreateYear,CreateMouth,IsShow,IsWWW,GoCount,ComeCount,UpdateTime,CreateTime ");
            strSql.Append(" FROM BlogInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageid"></param>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public DataSet GetList(int pagesize, int pageid, int totalcount, string strwhere)
        {
            int selectCount = 0;
            int tmp = totalcount - (pagesize * (pageid - 1));
            if (tmp > pagesize)
            {
                selectCount = pagesize * pageid;
            }
            else
            {
                pagesize = tmp;
                selectCount = totalcount;
            }

            string sqlStr;
            if (pageid == 1)
            {
                sqlStr = string.Format("select top {0} blogurl,blogname,blogtags,blogdesc,iswww from BlogInfo where {1} order by Score desc,blogid desc", pagesize, strwhere);
            }
            else
            {
                sqlStr = string.Format("select * from (select top {0} * from (select top {1} blogid,Score,blogurl,blogname,blogtags,blogdesc,iswww from BlogInfo where {2} order by Score desc,blogid desc) order by Score,blogid) order by Score desc,blogid desc",
                    pagesize, selectCount, strwhere);

                //sqlStr = string.Format("select top {0} blogurl,blogname,blogtags,blogdesc,iswww from BlogInfo where {1} and BlogID not in (" + "select top {2} BlogID from BlogInfo where {3} order by BlogID desc) order by BlogID desc",
                //    pagesize, strwhere, (pageindex - 1) * pagesize, strwhere);
            }

            //sqlStr = string.Format("{0} select count(0) from Article where {1}", sqlStr, strwhere);

            return DbHelperOleDb.Query(sqlStr.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BlogInfo ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.BlogUrl desc");
            }
            strSql.Append(")AS Row, T.*  from BlogInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "BlogInfo";
            parameters[1].Value = "BlogUrl";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

